/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import ProForm, {
  ProFormText,
  ProFormDatePicker,
  ProFormUploadButton,
  ProFormSwitch,
} from '@ant-design/pro-form';
import moment from 'moment';
import { importCov } from '@/services/Other/Cov19';
import type { ProFormInstance } from '@ant-design/pro-form';
import styles from '../index.less';
import { Col, message, Row, Space } from 'antd';

interface ImportCovProps {
  current: 'imp' | 'records' | string;
}

const layout = {
  labelCol: { span: 8 },
  wrapperCol: { span: 16 },
};

const yesterday = moment().subtract(1, 'days');

const ImportCov: React.FC<ImportCovProps> = () => {
  const formRef = useRef<ProFormInstance>();

  const disabledDate = (current: any) => {
    return current && current > moment().endOf('day');
  };

  const onFinish = async (data: any) => {
    const formData = new FormData();
    const { covid19_file = [], is_push, ...rest } = data;
    const params = {
      ...rest,
      is_push: is_push ? 1 : 0,
      covid19_file: covid19_file[0]?.originFileObj,
    };

    Object.keys(params).forEach((key) => {
      formData.append(key, params[key]);
    });
    const { code, msg } = await importCov(formData);
    if (code == 0) {
      formRef.current?.resetFields();
      message.success('提交成功！');
    } else {
      message.error(`提交失败：${msg}`);
    }
  };

  const beforeUpload = () => false;

  return (
    <ProForm
      formRef={formRef}
      layout='horizontal'
      initialValues={{
        date: yesterday,
      }}
      onFinish={onFinish}
      {...layout}
      submitter={{
        searchConfig: {
          submitText: '上传',
        },
        render: (props, doms) => {
          return (
            <Row>
              <Col span={14} offset={8}>
                <Space>{doms.reverse()}</Space>
              </Col>
            </Row>
          );
        },
      }}
    >
      <ProFormUploadButton
        className={styles.uploadButton}
        name='covid19_file'
        label='小区数'
        width='md'
        max={1}
        fieldProps={{
          beforeUpload,
        }}
        buttonProps={{
          type: 'primary',
        }}
        title='Excel表格批量导入'
        rules={[{ required: true, message: '请上传小区数' }]}
      />

      <ProFormDatePicker
        allowClear={false}
        fieldProps={{ disabledDate }}
        width='md'
        name='date'
        label='日期'
      />
      <ProFormText
        width='md'
        name='num'
        label='新增人数'
        rules={[{ required: true, message: '请填写新增人数' }]}
      />
      <ProFormText
        width='md'
        name='asymptomatic_num'
        label='无症状人数'
        rules={[{ required: true, message: '请填写新增无症状人数' }]}
      />
      <ProFormSwitch
        name='is_push'
        label='是否推送'
        fieldProps={{
          checkedChildren: '是',
          unCheckedChildren: '否',
        }}
      />
    </ProForm>
  );
};

export default ImportCov;
